﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using ViewMaker;
using ViewMaker.Core;

namespace CloudStorageLight.WpfClient
{
    [View(typeof(UserPasswordWindow))]
    public class UserPasswordVM : AppViewModel
    {
        public string Path { get; set; }
        public string User { get; set; }
        public String Password { get; set; }

        public bool Cancel { get; set; }

        public double FontSize { get; set; }

        public ICommand OK { get { return CreateCommand((x) => { ExecuteActionAsync(Login()); }); } }
            
        public ICommand Close { get { return CreateViewCommand("Close"); } }


        public UserPasswordVM()
        {
            var settings = BlobDataManager.Instance.LoadSettings();
            if(settings != null && !string.IsNullOrEmpty(settings.FontSize))
            {
                FontSize = (double)new FontSizeConverter().ConvertFrom(settings.FontSize);
            }
            else
            {
                FontSize = (double)new FontSizeConverter().ConvertFrom("12px");
            }
            Cancel = true;
        }
        private async Task Login()
        {
            try
            {                
                await BlobDataManager.Instance.PostCustomAuthInfo(Path, User, Password);
                this.ExecuteViewCommand("Close");
                Cancel = false;
            }
            catch (Exception ex)
            {
                this.ShowMessage(ex.Message);
            }
        }
    }
}
